package com.example.experminent3;

import android.os.Bundle;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ActionModeActivity extends AppCompatActivity {
    private ListView lvActionMode;
    private ActionModeAdapter adapter;
    private List<Map<String, Object>> dataList;
    private ActionMode.Callback actionModeCallback;
    private int selectedCount = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_action_mode);

        lvActionMode = findViewById(R.id.lv_action_mode);

        // 初始化数据
        initData();
        adapter = new ActionModeAdapter(this, dataList);
        lvActionMode.setAdapter(adapter);

        // 启用多选模式
        lvActionMode.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);

        // 配置ActionMode回调
        actionModeCallback = new ActionMode.Callback() {
            @Override
            public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                mode.getMenuInflater().inflate(R.menu.menu_action_mode, menu); // 加载上下文菜单
                return true;
            }

            @Override
            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                return false;
            }

            @Override
            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                if (item.getItemId() == R.id.menu_delete) {
                    // 处理删除逻辑
                    Toast.makeText(ActionModeActivity.this, "删除选中项", Toast.LENGTH_SHORT).show();
                    mode.finish(); // 关闭ActionMode
                    return true;
                }
                return false;
            }

            @Override
            public void onDestroyActionMode(ActionMode mode) {
                // 重置选中状态
                lvActionMode.clearChoices();
                adapter.notifyDataSetChanged();
                selectedCount = 0;
            }
        };

        // 设置ListView的多选回调
        lvActionMode.setMultiChoiceModeListener(new ListView.MultiChoiceModeListener() {
            @Override
            public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
                if (checked) {
                    selectedCount++;
                } else {
                    selectedCount--;
                }
                mode.setTitle(selectedCount + " selected"); // 更新选中数量标题
            }

            @Override
            public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                return actionModeCallback.onCreateActionMode(mode, menu);
            }

            @Override
            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                return actionModeCallback.onPrepareActionMode(mode, menu);
            }

            @Override
            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                return actionModeCallback.onActionItemClicked(mode, item);
            }

            @Override
            public void onDestroyActionMode(ActionMode mode) {
                actionModeCallback.onDestroyActionMode(mode);
            }
        });
    }

    // 初始化数据
    private void initData() {
        dataList = new ArrayList<>();
        String[] texts = {"One", "Two", "Three", "Four", "Five"};
        int[] icons = {
                R.mipmap.ic_launcher,
                R.mipmap.ic_launcher,
                R.mipmap.ic_launcher,
                R.mipmap.ic_launcher,
                R.mipmap.ic_launcher,
        };
        for (int i = 0; i < texts.length; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("text", texts[i]);
            map.put("icon", icons[i]);
            dataList.add(map);
        }
    }
}